<template>
  <div class="recommend" v-loading:[titleText]="loading">
    <Scroll class="recommend-content">
      <div>
         <div class="slider-wrapper">
          <div class="slider-content">
            <Slider v-if="sliders.length" :sliders="sliders"></Slider>
          </div>
        </div>
        <div class="recommend-list">
          <h1 class="list-title" v-show="!loading">热门歌单推荐</h1>
          <ul>
            <li v-for="item in albums" :key="item.id" class="item" @click="selectItem(item)">
              <div class="icon"><img v-lazy="item.pic" style="width:2.86rem;height: 2.86rem"></div>
              <div class="text">
                <h2 class="name">{{item.username}}</h2>
                <p class="title">{{item.title}}</p>
              </div>
            </li>
          </ul>
        </div>
      </div>
    </Scroll>
     <router-view v-slot="{ Component }">
      <transition name="slide">
        <component :is="Component" :data='selectedAlbum'/>
      </transition>
    </router-view>
  </div>
</template>
<script>
import { getRecommend } from '@/service/recommend.js'
import Slider from '@/components/base/slider/slider'
import Scroll from '@/components/base/scroll/scroll'
import storage from 'good-storage'
import { ALBUM_KEY } from '@/js/constant'
export default {
  name: 'Recommend',
  components: {
    Slider,
    Scroll
  },
  data () {
    return {
      sliders: [],
      albums: [],
      titleText: '正在加载...',
      selectedAlbum: null
    }
  },
  computed: {
    loading () {
      return !(this.sliders.length && this.albums.length)
    }
  },
  async created () {
    const result = await getRecommend()
    this.sliders = result.sliders
    this.albums = result.albums
  },
  methods: {
    selectItem (item) {
      this.selectedAlbum = item
      this.cacheAlbum(item)
      this.$router.push({
        path: `/recommend/${item.id}`
      })
    },
    cacheAlbum (album) {
      storage.set(ALBUM_KEY, album)
    }
  }
}
</script>
<style lang="scss" scoped>
  .recommend {
    position: fixed;
    width: 100%;
    top: 3.7527rem;
    bottom: 0;
    .recommend-content{
      height: 100%;
      overflow: hidden;
      .slider-wrapper {
        position: relative;
        width: 100%;
        height: 0;
        padding-top: 40%;
        overflow: hidden;
        .slider-content {
          position: absolute;
          left: 0;
          top: 0;
          width: 100%;
          height: 100%;
        }
      }
      .recommend-list {
        .list-title {
          height: 2.7719rem;
          line-height: 2.7719rem;
          text-align: center;
          font-size: $font-size-medium;
          color: $color-theme;
        }
        .item {
          display: flex;
          box-sizing: border-box;
          align-items: center;
          padding: 0 .8529rem .8529rem .8329rem;

          .icon {
            flex: 0 0 2.8586rem;
            width: 2.8586rem;
            padding-right: .9529rem;
          }
          .text {
            display: flex;
            flex-direction: column;
            justify-content: center;
            flex: 1;
            line-height: .8529rem;
            overflow: hidden;
            font-size: $font-size-medium;
          }
          .name {
            margin-bottom: .4264rem;
            color: $color-text;
          }
          .title {
            color: $color-text-d;
          }
        }
      }
    }
  }
</style>
